package com.dubbo.cp.consumer.controller;



import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dubbo.cp.IService.UserService;
import com.dubbo.cp.entity.UserEntity;


@Controller
public class IndexController extends BaseController {

	@Reference
	private UserService userServiceImpl;


	@RequestMapping("/")
	public String index() {
		return "/main/login";
	}

	@PostMapping("/login")
	public String login(@RequestParam String username,@RequestParam  String password,@RequestParam int role){
		System.out.println("/login");
		System.out.println("role=="+role);
		String url=null;
		//		// 判断用户名是否为空
		//		        boolean checkName=(null==username||"".equals(username));
		//		        // 判断密码是否为空
		//		        boolean checkPassord=(null==password||"".equals(password));
		//		        if(checkName||checkPassord){
		//		           // attributes.addFlashAttribute("nullNameAndPassword","用户名或密码不能为空");
		//		            return "/main/login";
		//		        }
		// 在数据库中查找
		if(role==1) {
			System.out.println("// 在数据库中查找");
			UserEntity user=userServiceImpl.getUser(username,password);
			if(user.getUsername()==null){   
				System.out.println("null");
				url = "/main/login";

			}else {
				System.out.println("role=1");
				url = "/main/index";
			}
		}else{
			UserEntity user=userServiceImpl.getUser(username,password);
			if(user==null||user.getUsername()=="admin"){   
				System.out.println("null");
				url = "/main/login";

			}else {
				System.out.println("role=2");
				url = "/index1";
			}
		}
		return url;



	}
}

